package com.crape.account.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.crape.account.bo.IAccountBO;
import com.crape.account.bo.IImageInfoBO;
import com.crape.account.bo.IUploadBO;
import com.crape.common.constants.ConstantCode;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.Account;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.DictProvince;
import com.crape.common.model.ImageInfo;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.tools.DESEncoder;
import com.crape.common.tools.IDESEncoder;
import com.crape.dict.bo.IAreaBO;
import common.Logger;

@Controller
public class UserSettingController {
	
	static Logger logger = Logger.getLogger(UserSettingController.class);

    private IAccountBO 		accountBOImpl;
    private IAreaBO 		areaBOImpl;
    private IUploadBO 		uploadBOImpl;
    private IImageInfoBO 	imageInfoBOImpl;
	
    @Autowired
    public void setAccountBOImpl(IAccountBO accountBOImpl) {
        this.accountBOImpl = accountBOImpl;
    }

    @Autowired
    public void setAreaBOImpl(IAreaBO areaBOImpl) {
        this.areaBOImpl = areaBOImpl;
    }
    
    @Autowired
	public void setUploadBOImpl(IUploadBO uploadBOImpl) {
		this.uploadBOImpl = uploadBOImpl;
	}
    
    @Autowired
	public void setImageInfoBOImpl(IImageInfoBO imageInfoBOImpl) {
		this.imageInfoBOImpl = imageInfoBOImpl;
	}
    
    @RequestMapping("/setting/modifyInfoInit.jsp")
    public String modifyInfoInit(ModelMap modelMap) {
        Long accountid = ThreadObjManager.getLoginUser().getAccountid();
        AccountInfo account = accountBOImpl.getAccountInfoById(accountid);
        List<DictProvince> provinceList = areaBOImpl.getAllProvinces();
        modelMap.put("account", account);
        modelMap.put("provinceList", provinceList);
        return "/setting/modifyInfo.html";
    }

    @RequestMapping("/setting/modifyInfo.jsp")
    public String modifyInfo(ModelMap modelMap, AccountInfo accountInfo) {
        accountBOImpl.updateInfo(accountInfo);
        List<DictProvince> provinceList = areaBOImpl.getAllProvinces();
        modelMap.put("account", accountInfo);
        modelMap.put("provinceList", provinceList);
        return "/setting/modifyInfo.html";
    }

    @RequestMapping("/setting/modifyImageInit.jsp")
    public String modifyImageInit(ModelMap modelMap) {
        Long accountid = ThreadObjManager.getLoginUser().getAccountid();
        AccountInfo account = accountBOImpl.getAccountInfoById(accountid);
        ImageInfo imageInfo = imageInfoBOImpl.queryImageInfoByName(account.getImagename());
        modelMap.put("imageInfo", imageInfo);
        return "/setting/modifyImage.html";
    }

    @RequestMapping(value="/user/uploadImages.jsp" ,method=RequestMethod.POST)
	public String uploadImages(HttpServletRequest request, @RequestParam("fileUpload")MultipartFile fileUpload, ModelMap modelMap) {
		try {
			ImageInfo imageInfo = uploadBOImpl.uploadImage(request, fileUpload, ConstantCode.imageSize);
			Long accountid = ThreadObjManager.getLoginUser().getAccountid();
			AccountInfo accountInfo = new AccountInfo();
			accountInfo.setAccountid(accountid);
			accountInfo.setImagename(imageInfo.getName());
			accountBOImpl.updateInfo(accountInfo);
			modelMap.put("imageInfo", imageInfo);
            return "/setting/modifyImage.html";
		} catch (BOException e) {
			e.printStackTrace();
			logger.error("uploadImages Error", e);
			modelMap.put("message", e.getMessage());
            return "/setting/modifyImage.html";
		}
    }
    
    @RequestMapping("/setting/modifyPasswordInit.jsp")
    public String modifyPasswordInit(ModelMap modelMap) {
        Long accountid = ThreadObjManager.getLoginUser().getAccountid();
        AccountInfo account = accountBOImpl.getAccountInfoById(accountid);
        modelMap.put("account", account);
        return "/setting/modifyPassword.html";
    }

    @RequestMapping("/setting/modifyPassword.jsp")
    public String modifyPassword(ModelMap modelMap, Account account) {
        if(account==null||account.getPassword()==null||account.getPassword().isEmpty()){
            modelMap.put("message", "请输入当前密码");
            return "/setting/modifyPassword.html";
        }
        if(account.getNewpassword()==null||account.getNewpassword().isEmpty()){
            modelMap.put("message", "请输入新密码");
            return "/setting/modifyPassword.html";
        }
        if(account.getConfirm()==null||account.getConfirm().isEmpty()){
            modelMap.put("message", "请输入确认密码");
            return "/setting/modifyPassword.html";
        }
        if(!account.getConfirm().equals(account.getNewpassword())){
            modelMap.put("message", "确认密码不一致");
            return "/setting/modifyPassword.html";
        }
        Long accountid = ThreadObjManager.getLoginUser().getAccountid();
        Account acct = accountBOImpl.getAccountById(accountid);
        if (acct==null) {
            modelMap.put("message", "用户不存在！");
            return "/setting/modifyPassword.html";
        }
        IDESEncoder encoder = DESEncoder.getInstance();
        String password = encoder.Encode(account.getPassword().trim());
        if (!acct.getPassword().equals(password)) {
            modelMap.put("message", "密码错误！");
            return "/setting/modifyPassword.html";
        }
        accountBOImpl.updatePassword(accountid, encoder.Encode(account.getNewpassword()));
        modelMap.put("message", "修改成功！");
        return "/setting/modifyPassword.html";
    }

}
